package com.googlecode.restitory.gae.hook;

import com.googlecode.restitory.gae.Request;
import com.googlecode.restitory.gae.RequestAdapter;
import com.googlecode.restitory.gae.Response;
import com.googlecode.restitory.gae.Type;

public abstract class AbstractHook implements Hook {

	private Chain chain;

	@Override
	public Response execute(Request request, Chain chain) {
		this.chain = chain;
		String path = request.getPath();
		Type type = request.getType();
		if (path.equals("/_/prepare") && type.equals(Type.PUT)) {
			return prepare(request);
		}
		if (path.equals("/_/destroy") && type.equals(Type.PUT)) {
			return destroy(request);
		}
		return handle(request);
	}

	public Response destroy(Request request) {
		return null;
	}

	public Response prepare(Request request) {
		return getChain().execute(request);
	}

	public abstract Response handle(Request request);

	public Chain getService() {
		return chain.copy();
	}

	public Chain getChain() {
		return chain;
	}

	public void setChain(Chain chain) {
		this.chain = chain;
	}

	public RequestAdapter getAdapter() {
		return new RequestAdapter(getService());
	}

}
